<?php
if (!class_exists("ImageLink")) include_once dirname(__FILE__).'/ImageLink.class.php';
if (!class_exists("URLVariables")) include_once dirname(__FILE__).'/../request/URLVariables.class.php';

class ShortList {	
	const FIRST_BUTTON_CONTENT 			= "firstButton";
	const PREVIOUS_BUTTON_CONTENT 		= "previousButton";
	const NEXT_BUTTON_CONTENT 			= "nextButton";
	const LAST_BUTTON_CONTENT 			= "lastButton";
	
	private $_itemRender;
	private $_sources;
	
	private $firstButtonEnable;
	private $previousButtonEnable;
	private $nextButtonEnable;
	private $lastButtonEnable;
	private $firstButtonDisable;
	private $previousButtonDisable;
	private $nextButtonDisable;
	private $lastButtonDisable;
	
	public $firstButton;
	public $previousButton;
	public $nextButton;
	public $lastButton;
	
	private $start;
	private $count;
	private $curentPage;
	private $allCount;
	private $countOnPage;
	private $varName;
	private $countPages;
	
	function __construct ($varName, $allCount, $countOnPage) {
		$this->curentPage = (isset($_GET[$varName]))?(int)$_GET[$varName]:0;
		$this->varName = $varName;
		$this->allCount = $allCount;
		$this->countOnPage = $countOnPage;
		$this->countPages = ceil($this->allCount/$this->countOnPage);
		if ($this->countPages > 0 && ($this->curentPage > $this->countPages-1 || $this->curentPage  < 0)){
			if ($this->curentPage > $this->countPages-1)
				$this->curentPage = $this->countPages-1;
			if ($this->curentPage  < 0)
				$this->curentPage = 0;
			$queryString = new URLVariables();
			$queryString->addVar($varName, $this->curentPage);
			header("location:".Config::SCRIPT_NAME()."?".$queryString->getString());
			exit();
		}
	}
	
	public function setEnable($first, $previous, $next, $last){
		$this->firstButtonEnable = $first;
		$this->previousButtonEnable = $previous;
		$this->nextButtonEnable = $next;
		$this->lastButtonEnable = $last;
	}
	
	public function setDisable($first, $previous, $next, $last){
		$this->firstButtonDisable = $first;
		$this->previousButtonDisable = $previous;
		$this->nextButtonDisable = $next;
		$this->lastButtonDisable = $last;
	}
	
	public function calcPosition(){
		$queryString = new URLVariables();
		
		$prevPage = $this->curentPage-1;
		if ($prevPage < 0)
			$prevPage = 0;
		$nextPage = $this->curentPage+1;
		if ($nextPage > $this->countPages-1 )
			$nextPage = $this->countPages-1;
		
		$array = array();
		$queryString->addVar($this->varName, 0, $_SERVER["QUERY_STRING"]);
		$this->firstButton = new ImageLink($this->firstButtonEnable, $queryString->getString("?"), '');
		$queryString->addVar($this->varName, $prevPage, $_SERVER["QUERY_STRING"]);
		$this->previousButton = new ImageLink($this->previousButtonEnable, $queryString->getString("?"), '');
		$queryString->addVar($this->varName, $nextPage, $_SERVER["QUERY_STRING"]);
		$this->nextButton = new ImageLink($this->nextButtonEnable, $queryString->getString("?"), '');
		$queryString->addVar($this->varName, $this->countPages-1, $_SERVER["QUERY_STRING"]);
		$this->lastButton = new ImageLink($this->lastButtonEnable, $queryString->getString("?"), '');
		
		if ($this->curentPage == 0){
			$this->firstButton = new ImageLink($this->firstButtonDisable, "#", '');
			$this->previousButton = new ImageLink($this->previousButtonDisable, "#", '');
		}
		
		if ($this->curentPage == $this->countPages-1 || $this->countPages == 0){
			$this->nextButton = new ImageLink($this->nextButtonDisable, "#", '');
			$this->lastButton = new ImageLink($this->lastButtonDisable, "#", '');
		}
		$array[] = $this->firstButton;
		$array[] = $this->previousButton;
		$array[] = $this->nextButton;
		$array[] = $this->lastButton;
		
		$this->_sources = $array;
	}
	
	public function getLimitSQL(){
		return "LIMIT ".$this->curentPage*$this->countOnPage.", ".$this->countOnPage;
	}
	
	public function getCurrentPage(){
		return $this->curentPage;
	}
	
	public function getAllCount() {
		return $this->allCount;
	}
	
	public function countOnPage() {
		return $this->countOnPage;
	}
	
	public function setItemRender($templateString){
		$this->_itemRender = $templateString;
	}
	public function getItemRender(){
		return $this->_itemRender;
	}
	
	public function draw($itemRender = ""){
		if ($itemRender != "")
			$this->_itemRender = $itemRender;
		$itemsArray = array();
		preg_match_all("/\{(.+?)\}/",$this->_itemRender, $itemsArray);		
		foreach ($this->_sources as $key => $item){
			$string = $this->_itemRender;
			$item = get_object_vars($item);
			foreach($itemsArray[1] as $k => $v)
				$string = str_replace($itemsArray[0][$k], $item[$v], $string);
			echo $string;
		}
	}
	
	public function getVarName(){
		return $this->varName;
	}
	
	public function getVars(){
		function getPublicObjectVars($obj) {
			return get_object_vars($obj);
		}
		return getPublicObjectVars($this);
	}
}

?>
